package com.ruoyi.light.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.ruoyi.common.domain.light.LitConfigErr;
import com.ruoyi.common.domain.light.LitConfigErrRole;
import com.ruoyi.light.mapper.LitConfigErrRoleMapper;
import com.ruoyi.light.service.ILitConfigErrRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.light.service.ILitConfigErrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户和角色关联 服务层实现
 *
 * @author ruoyi
 * @date 2022-02-22
 */
@Service
public class LitConfigErrRoleServiceImpl extends ServiceImpl<LitConfigErrRoleMapper, LitConfigErrRole> implements ILitConfigErrRoleService {
    @Autowired
    private ILitConfigErrService configErrService;

    @Override
    public List<Long> getAllRoleIdsErrId(Long errId) {
        List<Long> errIdAllList = getAllErrIdListByErrId(errId);
        List<LitConfigErrRole> list = ChainWrappers.lambdaQueryChain(this.baseMapper)
                .in(LitConfigErrRole::getErrId, errIdAllList)
                .list();
        if (list.size() == 0) {
            return new ArrayList<>();
        }
        return list.stream().map(LitConfigErrRole::getRoleId).collect(Collectors.toList());
    }


    @Override
    public List<Long> getAllErrIdsRoleIds(List<Long> roleIdList) {
        List<LitConfigErrRole> list = ChainWrappers.lambdaQueryChain(this.baseMapper)
                .in(LitConfigErrRole::getRoleId, roleIdList)
                .list();
        if (list.size() == 0) {
            return new ArrayList<>();
        }
        List<Long> collect = list.stream().map(LitConfigErrRole::getErrId).collect(Collectors.toList());
        return getAllErrIdByErrIdList(collect);
    }


    @Override
    public boolean exists(Long errId, List<Long> roleIdList) {
        List<Long> errIdAllList = getAllErrIdListByErrId(errId);
        return ChainWrappers.lambdaQueryChain(this.getBaseMapper())
                .in(LitConfigErrRole::getErrId, errIdAllList)
                .in(LitConfigErrRole::getRoleId, roleIdList).exists();
    }


    private List<Long> getAllErrIdByErrIdList(List<Long> errIdList) {
        List<Long> errIdAllList = new ArrayList<>(errIdList);
        addAllParentErrId(errIdAllList, errIdList);
        return errIdAllList;
    }

    private List<Long> getAllErrIdListByErrId(Long errId) {
        List<Long> errIdAllList = new ArrayList<>(List.of(errId));
        addAllParentErrId(errIdAllList, errId);
        return errIdAllList;
    }


    private void addAllParentErrId(List<Long> errIdAllList, List<Long> errIds) {
        List<LitConfigErr> err = configErrService.listByIds(errIds);
        if (err != null && err.size() > 0) {
            List<Long> parentIds = err.stream().filter(t -> t.getParentId() != null && t.getParentId() > 0)
                    .map(LitConfigErr::getParentId).collect(Collectors.toList());
            if (parentIds.size() > 0) {
                errIdAllList.addAll(parentIds);
                addAllParentErrId(errIdAllList, parentIds);
            }
        }
    }

    private void addAllParentErrId(List<Long> errIdAllList, Long errId) {
        LitConfigErr err = configErrService.getById(errId);
        if (err != null && err.getParentId() != null && err.getParentId() > 0) {
            errIdAllList.add(err.getParentId());
            addAllParentErrId(errIdAllList, err.getParentId());
        }
    }

}